<?php
class Muser extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="users";
    }
    
    public function listAll($amount, $start) {
        $query = $this->db->query("select u.*, l.level_name from users as u
                            inner join levels as l on l.level_id = u.level_id order by u.u_id desc limit $start, $amount");
        if($query->num_rows()>0)
            return $query->result_array();
        else
            return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function getById($id) {
        $this->db->where("u_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return $query->row_array();
        else
            return false;
    }
    
    public function search($username, $fullname, $address, $phone, $email, $level, $amount, $start) {
        $u=$f=$a=$p=$e=$l="'1=1'";
        if($username!="")$u="u_username like '%".$username."%'";
        if($fullname!="")$f="u_fullname like '%".$fullname."%'";
        if($address!="")$a="u_address like '%".$address."%'";
        if($phone!="")$p="u_phone like '%".$phone."%'";
        if($email!="")$u="u_email like '%".$email."%'";
        if($level!="")$f="u.level_id = '".$level."'";
        $str="select u.*, l.level_name from users as u inner join levels as l on l.level_id = u.level_id where $u and $f and $a and $p and $e and $l limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($username, $fullname, $address, $phone, $email, $level) {
        $u=$f=$a=$p=$e=$l="1=1";
        if($username!="")$u="u_username like '%".$username."%'";
        if($fullname!="")$f="u_fullname like '%".$fullname."%'";
        if($address!="")$a="u_address like '%".$address."%'";
        if($phone!="")$p="u_phone like '%".$phone."%'";
        if($email!="")$u="u_email like '%".$email."%'";
        if($level!="")$f="level_id = '".$level."'";
        $str="select count(*) as 'count' from users where $u and $f and $a and $p and $e and $l";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("u_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("u_id", $id);
        $this->db->delete($this->_table);
    }
    
    public function checkUsername($username, $id="") {
        if($id!="")
            $this->db->where("u_id != ", $id);
        $this->db->where("u_username", $username);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
    
    public function checkEmail($email, $id="") {
        if($id!="")
            $this->db->where("u_id != ", $id);
        $this->db->where("u_email", $email);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
    
    public function login($username, $password) {
        $this->db->where("u_username", $username);
        $this->db->where("u_password", $password);
        $query=$this->db->get($this->_table);
        if($query->num_rows() > 0) return $query->row_array(); else return false;
    }
}
?>
